package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dj.dormitory.commonutils.JWTUtil;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.Menu;
import com.dj.dormitory.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    //获得所有菜单
    @GetMapping("/list")
    public Result list(HttpServletRequest request){

        Integer type = (Integer) request.getAttribute("type");
        //根据token 获得 type
        List<Menu> tree =  menuService.getTree(type);
        if(tree!=null){
            System.out.println(tree.size());
        }else{
            System.out.println("树是空的");
        }

        return Result.ok().data("menuList",tree);
    }



    @GetMapping("/getAll")
    public Result getAll(){
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        List<Menu> list = menuService.getMenuByType(-1);
        return Result.ok().data("menu",list);
    }
    //根据type 返回ids
    @PostMapping("/checked/{type}")
    public Result returnChecked(@PathVariable String type){

        List<Menu> list = menuService.getMenuByType(Integer.parseInt(type));
        List<Integer> ids = new ArrayList<>();
        for(Menu one:list){
            ids.add(one.getId());
        }
        return Result.ok().data("ids",ids);
    }
}

